package com.cashmama.market.api.application.controller.config;

import com.alibaba.fastjson.JSONObject;
import com.cashmama.market.api.application.annos.VisitorAccessible;
import com.cashmama.market.api.application.controller.config.response.ConfigAppCodeResponse;
import com.cashmama.market.api.application.controller.config.response.RegisterProtocolResponse;
import com.cashmama.market.api.domain.utils.AppUtils;
import com.cashmama.market.api.domain.vo.RequestLocalInfo;
import com.cashmama.market.api.infras.app.RequestBasicInfo;
import com.cashmama.market.api.infras.config.ApolloConfigUtil;
import com.cashmama.market.api.infras.enums.PackageNameEnum;
import com.cashmama.market.api.infras.handler.ApiResponse;
import com.fqgj.common.utils.MD5;
import com.qianli.cashmama.product.facade.result.AppVo;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * @program: market-api
 * @description
 * @author: frank
 * @create: 2019-12-24 11:57
 **/
@RestController
@RequestMapping("/config")
public class ConfigController {
    private static final Logger LOGGER = LoggerFactory.getLogger(ConfigController.class);

    @Autowired
    private AppUtils appUtils;
    @Autowired
    private ApolloConfigUtil apolloConfigUtil;

    @VisitorAccessible
    @RequestMapping(value = "/appcode/get", method = RequestMethod.POST)
    public ApiResponse<ConfigAppCodeResponse> configGetAppCode() {
        RequestBasicInfo requestBasicInfo = RequestLocalInfo.getRequestBasicInfo();
//        AppVo appVo = appUtils.getAppVo();
//        ConfigAppCodeResponse configAppCodeResponse = new ConfigAppCodeResponse();
//        configAppCodeResponse.setAppCode(apolloConfigUtil.getRuleCode());
//        configAppCodeResponse.setContactsTip(appVo.getDisplayName() + " need reference contact for loan processing");
//
//        if (StringUtils.isEmpty(requestBasicInfo.getGuestId())) {
//            configAppCodeResponse.setGuestId(MD5.md5(java.util.UUID.randomUUID().toString()));
//        }
//
//        int versionCode = requestBasicInfo.getVersionCode() == null ? 0 : requestBasicInfo.getVersionCode();
//        String appClient = requestBasicInfo.getAppClient();
//        String packageName = requestBasicInfo.getPackageName();
//        String menuInfo = apolloConfigUtil.getUserInfoMenu();
//        LOGGER.info("==== config info menuInfo:{}====", menuInfo);
//        JSONObject jsonObject = JSONObject.parseObject(menuInfo);
//        int androidMinVersionCode = appUtils.getAndroidMinVersionCode(packageName, jsonObject);

//        if ("iphone".equalsIgnoreCase(appClient) && versionCode < apolloConfigUtil.getIosMinVersionCode()) {
//            configAppCodeResponse.setForceUpdateDesc(apolloConfigUtil.getForceUpdateDesc());
//            configAppCodeResponse.setForceUpdateUrl(apolloConfigUtil.getForceUpdateIosUrl());
//            configAppCodeResponse.setForceUpdate(false);
//        } else if ("android".equalsIgnoreCase(appClient) && versionCode < androidMinVersionCode) {
//            configAppCodeResponse.setForceUpdateDesc(apolloConfigUtil.getForceUpdateDesc());
//            configAppCodeResponse.setForceUpdateUrl(apolloConfigUtil.getForceUpdateAndroidUrl());
//            configAppCodeResponse.setForceUpdate(appUtils.isForceUpdate(packageName, jsonObject));
//            configAppCodeResponse.setReviewUrl(appUtils.getStoreAppReviewUrl());
//            configAppCodeResponse.setNeedUpdate(true);
//            configAppCodeResponse.setForceUpdateDescLayout(apolloConfigUtil.getForceUpdateDescLayout());
//        }
//        configAppCodeResponse.setGpsOpen(apolloConfigUtil.gpsOpen());
        return new ApiResponse<>(new ConfigAppCodeResponse()).setMsg("获取成功");
    }

    /**
     * 注册协议与隐私保护政策
     *
     * @return
     */
    @VisitorAccessible
    @RequestMapping(value = "/protocol/get", method = RequestMethod.POST)
    public ApiResponse<RegisterProtocolResponse> getRegisterProtocal() {
        String packageName = RequestLocalInfo.getRequestBasicInfo().getPackageName();
//        String menuInfo = apolloConfigUtil.getUserInfoMenu();
//        JSONObject jsonObject = JSONObject.parseObject(menuInfo);
//        String registerProtolUrl = appUtils.getRegisterProtocol(packageName, jsonObject);
//        String privacyPolicyUrl = appUtils.getPrivacyPolicyUrl(packageName, jsonObject);
//        String termsOfServicesUrl = appUtils.getTermsOfServiceUrl(packageName, jsonObject);
        String registerProtolUrl;
        String privacyPolicyUrl;
        String termsOfServicesUrl;
        if (packageName.equals(PackageNameEnum.CASHMall.getPackageName())) {
            registerProtolUrl = "https://www.cash-mall.in/termsOfService";
            privacyPolicyUrl = "https://www.cash-mall.in//privacypolicy";
            termsOfServicesUrl = "https://www.cash-mall.in/termsOfService";
        }else {
            registerProtolUrl = "https://www.rupee-mama.in/termsOfService";
            privacyPolicyUrl = "https://www.rupee-mama.in//privacypolicy";
            termsOfServicesUrl = "https://www.rupee-mama.in/termsOfService";
        }

        RegisterProtocolResponse registerProtocolVO = new RegisterProtocolResponse(registerProtolUrl, privacyPolicyUrl, termsOfServicesUrl);
        return new ApiResponse<>(registerProtocolVO);
    }
}
